﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Engine.Gameplay.ObjectModel;
using Engine.Rendering;
using Microsoft.Xna.Framework.Content;

namespace Engine.Gameplay.GameWorld
{
    public class World : IWorld
    {
        #region Properties
        public ICollection<IGameObject> GameObjects { get; private set; }
        public ICamera Camera { get; set; }
        public RenderingSystem RenderingSystem { get; private set; }
        #endregion // Properties


        #region Constructors
        public World(RenderingSystem renderingSystem)
        {
            this.RenderingSystem = renderingSystem;
            this.GameObjects = new List<IGameObject>();
        }
        #endregion // Constructors


        #region Methods
        public void Update(GameTime gameTime)
        {
            if (this.GameObjects == null) return;

            foreach (var o in this.GameObjects)
                o.Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            if (Camera != null && GameObjects != null && RenderingSystem != null)
            {
                foreach (var o in GameObjects)
                {
                    if (o is IModeledGameObject)
                    {
                        var mo = (IModeledGameObject)o;
                        RenderingSystem.DrawModel(mo.Model, Camera, mo);
                    }
                    else
                        RenderingSystem.Draw(gameTime, Camera, o);
                }
            }
        }

        public void Dispose()
        {
            for (int i = this.GameObjects.Count - 1; i >= 0; --i)
                this.GameObjects.ElementAt(i).Dispose();
            this.GameObjects.Clear();
        }
        #endregion // Methods

    }
}
